// ~ CopyRight © 2012 USTC SINOVATE  SOFTWARE CO.LTD All Rights Reserved.
package com.cmcc.zysoft.sysmanage.controller;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Department;
import com.cmcc.zysoft.infocollection.model.Employee;
import com.cmcc.zysoft.infocollection.model.Headship;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.model.UserRole;
import com.cmcc.zysoft.infocollection.util.MD5Tools;
import com.cmcc.zysoft.infocollection.util.UUIDUtil;
import com.cmcc.zysoft.spring.security.model.User;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;
import com.cmcc.zysoft.sysmanage.service.CompanyService;
import com.cmcc.zysoft.sysmanage.service.DepartmentService;
import com.cmcc.zysoft.sysmanage.service.SysEmployeeService;
import com.cmcc.zysoft.sysmanage.service.SystemUserPCService;
import com.cmcc.zysoft.sysmanage.service.UserRoleService;
import com.starit.common.dao.support.Pagination;

/**
 * 系统用户
 * @author 闫豆
 * @date 2012-11-28 下午7:56:07
 */

@Controller
@RequestMapping("/pc/systemUser")
public class SystemUserPCController extends BaseController {
	
	/**
	 * 属性名称：_logger 类型：Logger
	 */
	private static Logger _logger = LoggerFactory.getLogger(SystemUserPCController.class);
	
	/**
	 * 属性名称：systemUserPCService 类型：SystemUserPCService
	 */
	@Resource
	private SystemUserPCService systemUserPCService;
	
	/**
	 * 属性名称：userRoleService 类型：UserRoleService
	 */
	@Resource
	private UserRoleService userRoleService;
	
	/**
	 * 属性名称：departmentService 类型：DepartmentService
	 */
	@Resource
	private SysEmployeeService sysEmployeeService;
	
	/**
	 * 属性名称：departmentService <br/>
	 * 类型：DepartmentService
	 */
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 公司信息业务层
	 */
	@Resource
	private CompanyService companyService;
	
	/**
	 * testImgAreaSelect
	 * 
	 * @return 返回类型：String
	 */
	@RequestMapping("/testImgAreaSelect")
	public String testImgAreaSelect(){
		return "sysmanage/systemUser/testImgAreaSelect";
	}
	
		
	/**
	 * 跳转到系统用户管理页面
	 * @return
	 */
	@RequestMapping("/main.htm")
	public String systemUser(){
		return "sysmanage/systemUser/systemUser";
	}
	
	
	/**
	 * 跳转到修改密码页面
	 * @return
	 */
	@RequestMapping("/password.htm")
	public String password(){
		return "sysmanage/systemUser/password";
	}
	
	/**
	 * 修改密码
	 * PS：修改密码要修改两个地方，一个是adc服务器上的密码，一个是销售管家本地服务器的密码
	 * @return 返回类型：Map<String,String>
	 */
	@RequestMapping("/savePassword.htm")
	@ResponseBody
	public Map<String, String> savePassword(String oldPassword,String newPassword,HttpServletRequest request){
		Map<String, String>  map = new HashMap<String, String>();
		User user = SecurityContextUtil.getCurrentUser();
		//判断原始密码输入的是否正确
		if(!MD5Tools.isPasswordValid(user.getPassword(), oldPassword, user.getPassSalt())){
			map.put("success", "false");
			map.put("msg", "旧密码输入不正确");
			return map;
		}
		//修改本地密码
		SystemUser systemUser = systemUserPCService.getEntity(user.getUserId());
		String passwordSalt = UUIDUtil.generateUUID();
		systemUser.setPassSalt(passwordSalt);
		systemUser.setPassword(MD5Tools.encodePassword(newPassword, passwordSalt));
		systemUser.setModifyTime(new Date());
		systemUserPCService.updateSystemUser(systemUser);
		map.put("success", "true");
		return map;
	}
	
	/**
	 * 获取所有用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/all.htm", method = RequestMethod.POST)
	@ResponseBody
	public Pagination<Object> getAllUser(@RequestParam("departmentid") String departmentid , int page , int rows) {
		_logger.debug("获取所有用户信息");
		return this.systemUserPCService.getAllUser(page,rows,departmentid);
	}
	
	/**
	 * 跳转至用户添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add.htm")
	public String add() {
		return "/sysmanage/systemUser/addSystemUser";
	}
	
	/**
	 * 添加用户信息
	 * 
	 * @param systemUser 用户对象
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/save.htm", method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public String saveSystemUser(SystemUser systemUser) {
		_logger.debug("添加用户信息");
		try{
			boolean check=this.systemUserPCService.isUserNameEqual(systemUser.getUserName(),"0");//添加时判断用户名是否相同
			if(check==true){
				return "2";
			}
			boolean checkPhoneNum=this.systemUserPCService.isPhoneNumexist(systemUser.getMobile() , "0");//判断手机号码是否存在
			if(checkPhoneNum==true){
				return "3";
			}
			String salt = UUIDUtil.generateUUID();//取密码盐
			String password = MD5Tools.encodePassword("111111", salt);//得到加密后的密码,添加时默认密码111111
			systemUser.setPassSalt(salt);
			systemUser.setPassword(password);
			systemUser.setDelFlag("0");
			this.systemUserPCService.insertEntity(systemUser);
		}catch(Exception e){
			_logger.error("添加用户信息失败");
			e.printStackTrace();
			return "0";
		}
		return "1";
	}
	
	/**
	 * 跳转至修改用户信息页面
	 * 
	 * @param systemUserId 用户id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit.htm")
	public String update(@RequestParam("systemUserId") String systemUserId, ModelMap modelMap) {
		_logger.debug("跳转至修改员工信息页面");
		SystemUser systemUser = this.systemUserPCService.getSystemUser(systemUserId);
 	    modelMap.addAttribute("systemUser", systemUser);
		return "sysmanage/systemUser/editSystemUser";
	}
	
	/**
	 * 修改用户信息
	 * @param systemUser 用户对象
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editSystemUser.htm", method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public String updateSystemUser(SystemUser systemUser) {
		_logger.debug("修改用户信息");		
		try {
			boolean check=this.systemUserPCService.isUserNameEqual(systemUser.getUserName(),systemUser.getUserId());//添加时判断用户名是否相同
			if(check==true){
				return "2";
			}
			boolean checkPhoneNum=this.systemUserPCService.isPhoneNumexist(systemUser.getMobile(),systemUser.getUserId());//判断手机号码是否存在
			if(checkPhoneNum==true){
				return "3";
			}
			this.systemUserPCService.updateSystemUser(systemUser);
		} catch (Exception e) {
			_logger.error("修改用户信息失败");
			return "0";
		}
		return "1";
	}
	
	/**
	 * 删除用户信息
	 * 
	 * @param systemUserId 用户id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delSystemUser.htm", method = RequestMethod.GET)
	@ResponseBody
	@Transactional
	public String deleteEmployee(@RequestParam("systemUserId") String systemUserId) {
		_logger.debug("删除用户信息");
		try {
			  String[] systemUserIds = systemUserId.split(",");
				for(int j=0;j<systemUserIds.length;j++){
					this.systemUserPCService.delSystemUser(systemUserIds[j]);
				}
		} catch (Exception e) {
			_logger.error("删除用户信息失败");
			return "0";
		}
		return "1";
	}
	
	/**
	 * 得到所有公司的名称
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value = "/allCompany.htm", method = RequestMethod.POST)
	@ResponseBody
	public Pagination<Object> getAllCompany(int page , int rows) {
		_logger.debug("得到所有公司");
		return this.systemUserPCService.getAllCompany(page,rows);
	}
}
